package hit.reports;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * A Table to be including when building a report.
 *
 */
public class BuilderTable extends BuilderObject {

	/**
	 * The list of all columns for the table.
	 */
	private List<TableColumn> columns;
	
	/**
	 * The list of rows for the table.
	 */
	private List<TableRow> rows;
	
	/**
	 * Default Constructor
	 */
	public BuilderTable()
	{
		columns = new ArrayList<TableColumn>();
		rows = new ArrayList<TableRow>();
	}
	
	/**
	 * Adds a new {@link TableColumn} to the table.
	 * @param column The column to be added.
	 * @throws IllegalArgumentException if the column is null, or already added to the table.
	 */
	public void AddColumn(TableColumn column) throws IllegalArgumentException
	{
		if (column == null || columns.contains(column))
			throw new IllegalArgumentException();
		
		columns.add(column);
	}
	
	/**
	 * Adds a new {@link TableRow} to the table.
	 * @param row The row to be added.
	 * @throw IllegalArgumentException if the row is null, or already added to the table.
	 */
	public void AddRow(TableRow row) throws IllegalArgumentException
	{
		if (row == null || rows.contains(row))
			throw new IllegalArgumentException();
		
		rows.add(row);
	}
	
	/**
	 * Gets a {@link TableColumn} from the table for a given index.
	 * @param index The index of the column in the table.
	 * @return The column for the given index.
	 * @throws IllegalArgumentException If the index is out of range.
	 */
	public TableColumn GetColumn(int index) throws IllegalArgumentException
	{
		if (index < 0 || index >= columns.size())
			throw new IllegalArgumentException();
		
		return columns.get(index);
	}
	
	/**
	 * Get a {@link TableRow} from the table for a given index
	 * @param index The index of the row in the table.
	 * @return The row for the given index.
	 * @throws IllegalArgumentException If the index is out of range.
	 */
	public TableRow GetRow(int index) throws IllegalArgumentException
	{
		if (index < 0 || index >= rows.size())
			throw new IllegalArgumentException();
		
		return rows.get(index);
	}
	
	/**
	 * Gets the iterator for all {@link TableColumn}s in the table.
	 * @return The iterator for the columns
	 */
	public Iterator<TableColumn> GetColumnIterator()
	{
		return columns.iterator();
	}
	
	/**
	 * Gets the iterator for all {@link TableRow}s in the table.
	 * @return The iterator for the rows.
	 */
	public Iterator<TableRow> GetRowIterator()
	{
		return rows.iterator();
	}
	
	/**
	 * Gets the number of columns currently in the table.
	 * @return the number of columns.
	 */
	public int GetColumnCount()
	{
		return columns.size();
	}
	
	/**
	 * Gets the number of rows currently in the table.
	 * @return the number of rows.
	 */
	public int GetRowCount()
	{
		return rows.size();
	}
	
	/**
	 * Removes a {@link TableColumn} from the table.
	 * @param column The column to be removed
	 * @throws IllegalArgumentException if column is null
	 */
	public void RemoveColumn(TableColumn column) throws IllegalArgumentException
	{
		if (column == null)
			throw new IllegalArgumentException();
		
		columns.remove(column);
	}
	
	/**
	 * Removes a {@link TableRow} from the table.
	 * @param row The row to be removed
	 * @throws IllegalArgumentException if row is null
	 */
	public void RemoveRow(TableRow row) throws IllegalArgumentException
	{
		if (row == null)
			throw new IllegalArgumentException();
		
		rows.remove(row);
	}
}
